package com.fw.ui.bao;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

import java.io.Serializable;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.TimeoutException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.fw.service.bean.EntryBean;
import com.fw.service.message.MessageService;
import com.fw.ui.service.ServiceExecutor;
import com.fw.ui.service.ServiceOrder;

@Component
@Scope(SCOPE_PROTOTYPE)
public class MessageBAO extends AbstractBAO implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public static final String MESSAGE_INFO_TYPE = "MESSAGE_INFO_TYPE";
    public static final String MESSAGE_WARNING_TYPE = "MESSAGE_WARNING_TYPE";
    public static final String MESSAGE_ERROR_TYPE = "MESSAGE_ERROR_TYPE";

    @Autowired
    private MessageService messageService;

    private static Map<String, String> messageMap = new TreeMap<String, String>();

    @SuppressWarnings("unchecked")
    public boolean initialSystemMessage(Locale locale) {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.messageService);
	    serviceOrder.setMethodName("getMessageAll");
	    serviceOrder.setParameterTypes(new Class[] { Locale.class });
	    serviceOrder.setParameterValues(new Object[] { locale });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		List<EntryBean<String, String>> entryBeans = (List<EntryBean<String, String>>) serviceOrder.getReturnValue();
		if (entryBeans != null && !entryBeans.isEmpty()) {
		    for (EntryBean<String, String> entry : entryBeans) {
			messageMap.put(entry.getKey(), entry.getValue());
		    }
		    return true;
		} else {
		    return false;
		}
	    }
	    return false;
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return false;
    }

    public synchronized String getMessage(String key) {
	if (!messageMap.isEmpty()) {
	    return messageMap.get(key);
	}
	return null;
    }

    public String getMessage(String code, Locale locale) throws TimeoutException {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.messageService);
	    serviceOrder.setMethodName("getMessage");
	    serviceOrder.setParameterTypes(new Class[] { String.class, Locale.class });
	    serviceOrder.setParameterValues(new Object[] { code, locale });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		String result = (String) serviceOrder.getReturnValue();
		return result;
	    }
	} catch (TimeoutException e) {
	    throw e;
	} catch (Exception e) {

	}
	return null;
    }

    public String getMessageWithDefaultMessage(String code, String defaultMessage, Locale locale) throws TimeoutException {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.messageService);
	    serviceOrder.setMethodName("getMessageWithDefaultMessage");
	    serviceOrder.setParameterTypes(new Class[] { String.class, String.class, Locale.class });
	    serviceOrder.setParameterValues(new Object[] { code, defaultMessage, locale });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		String result = (String) serviceOrder.getReturnValue();
		return result;
	    }
	} catch (TimeoutException e) {
	    throw e;
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return null;
    }

    public synchronized String getMessageWithParams(String code, Object[] args) {
	try {
	    if (!messageMap.isEmpty()) {
		String message = messageMap.get(code);
		if (args != null && args.length > 0) {
		    for (int loop = 0; loop < args.length; loop++) {
			if (args[loop] == null) {
			    continue;
			}
			String parameterS = messageMap.get(args[loop]);
			if (parameterS != null) {
			    message = message.replaceAll("\\{" + loop + "\\}", parameterS);
			} else {
			    try {
				message = message.replaceAll("\\{" + loop + "\\}", "" + args[loop]);
			    } catch (Exception e) {
				message += " regular expression: " + args[loop];
			    }
			}
		    }
		}
		return message;
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	}
	return null;
    }

    public String getMessageWithParams(String code, Object[] args, Locale locale) throws TimeoutException {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.messageService);
	    serviceOrder.setMethodName("getMessageWithParams");
	    serviceOrder.setParameterTypes(new Class[] { String.class, Object[].class, Locale.class });
	    serviceOrder.setParameterValues(new Object[] { code, args, locale });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		String result = (String) serviceOrder.getReturnValue();
		return result;
	    }
	} catch (TimeoutException e) {
	    throw e;
	} catch (Exception e) {

	}
	return null;
    }

    public String getMessageWithParamsWithDefaultMessage(String code, Object[] args, String defaultMessage, Locale locale)
	    throws TimeoutException {
	try {
	    ServiceOrder serviceOrder = new ServiceOrder();
	    serviceOrder.setServiceClass(this.messageService);
	    serviceOrder.setMethodName("getMessageWithParamsWithDefaultMessage");
	    serviceOrder.setParameterTypes(new Class[] { String.class, Object[].class, String.class, Locale.class });
	    serviceOrder.setParameterValues(new Object[] { code, args, defaultMessage, locale });

	    ServiceExecutor serviceExecutor = new ServiceExecutor(serviceOrder, timeout);
	    serviceOrder = serviceExecutor.callService();
	    if (serviceOrder.getResultCode() != null && serviceOrder.getResultCode().equals(new Integer(0))) {
		String result = (String) serviceOrder.getReturnValue();
		return result;
	    }
	} catch (TimeoutException e) {
	    throw e;
	} catch (Exception e) {

	}
	return null;
    }

}
